﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CMS.BLL;

public partial class Controls_News_ctrNewsDetails : System.Web.UI.UserControl
{
    private int nCurentLanguage = Support.GetCurrentLanguage() - 1;
    private int nDefaultLanguage = Support.GetDefaultLanguage();
    int CurrentPageIndex = 1;
    private int nItenView = 0;
    private int nPageCount = 0;
    int nPageSize = 10;
    private string sTitle = string.Empty ;
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            this.ShareLink();
            this.LoadData();
        }
    }
    private void ShareLink()
    {

        hplFaceBook.NavigateUrl = "https://www.facebook.com/sharer/sharer.php?u=" + System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
        hplGoogle.NavigateUrl = "https://www.google.com/bookmarks/mark?op=edit&amp;bkmk=" + System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
        hplTwitter.NavigateUrl = "http://twitter.com/intent/session?original_referer=" + System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
    }
    private void LoadData()
    {
        try
        {
            
            int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.NEWS_ID]);

            News objNews = new News();
            objNews.LoadById(nItemID);
            if (nItemID == 0)
            {
                string sURL = Config.ROOT + Pages.FrontEnds.HOME;
                Response.Redirect(sURL);
            }
            else
            {
                switch (nCurentLanguage)
                {
                    case 1:
                        lblTitle.Text = objNews.Data.NewsTitle1;

                        ltrContent.Text = objNews.Data.MainContent1;
                        lblInitData.Text = objNews.Data.InitContent1;

                        Page.Title = objNews.Data.NewsTitle1;
                        break;
                    case 2:
                        lblTitle.Text = objNews.Data.NewsTitle2;

                        ltrContent.Text = objNews.Data.MainContent2;
                        lblInitData.Text = objNews.Data.InitContent2;

                        Page.Title = objNews.Data.NewsTitle2;
                        break;
                    case 3:
                        lblTitle.Text = objNews.Data.NewsTitle3;

                        ltrContent.Text = objNews.Data.MainContent3;
                        lblInitData.Text = objNews.Data.InitContent3;

                        Page.Title = objNews.Data.NewsTitle3;
                        break;
                }

                lblDatePosted.Text = MultipleLanguage.POSTED_DATE[nCurentLanguage - 1] + ConvertData.ConvertIntToDatetimeString(objNews.Data.CreatedDate);
             
                lblSource.Text = objNews.Data.Source;
               


                NewsComment objNewsComment = new NewsComment();
                DataTable dtbNewsComment = new DataTable();
                dtbNewsComment = objNewsComment.Search(nItenView, sTitle, nItemID, 1, 1, 10, ref nPageCount);
                if (dtbNewsComment.Rows.Count > 0)
                {
                    rptComment.DataSource = dtbNewsComment;
                    rptComment.DataBind();
                    divComment.Visible = true;
                    lblComment.Text = "Bình luận cho bài viết này" + " " + "(" + dtbNewsComment.Rows.Count + " " + "bình luận" + ")";
                }
                else
                {
                    divComment.Visible = false;
                }

                int nIDCat = objNews.Data.NewsCategoryID;

                DataTable dtb = new DataTable();
                dtb = objNews.Search(10, nItemID, "", nIDCat, nDefaultLanguage, nCurentLanguage, EnumeType.ACTIVE, CurrentPageIndex, nPageSize, ref nPageCount);
                rptRelatedNews.DataSource = dtb;
                rptRelatedNews.DataBind();
 


                lblContent.Text = MultipleLanguage.CONTENT[nCurentLanguage - 1];
                lblUsername.Text = MultipleLanguage.FULLNAME[nCurentLanguage - 1];
                lblEmail.Text = MultipleLanguage.EMAIL[nCurentLanguage - 1];
                lblCommentInfo.Text = MultipleLanguage.COMMENT_INFO[nCurentLanguage - 1];
                btnSave.Text = MultipleLanguage.BTN_SAVE[nCurentLanguage - 1];
                rfvUsername.ErrorMessage = MultipleLanguage.USERNAME_EMPTY[nCurentLanguage - 1];
                rfvEmail.ErrorMessage = MultipleLanguage.EMAIL_EMPTY[nCurentLanguage - 1];
                revEmail.ErrorMessage = MultipleLanguage.EMAIL_WRONG[nCurentLanguage - 1];
                rfvContent.ErrorMessage = MultipleLanguage.CONTENT_EMPTY[nCurentLanguage - 1];
                lbNote.Text = MultipleLanguage.COMMENT_NOTE[nCurentLanguage - 1];

                lbnewsother.Text = MultipleLanguage.NEWS_OTHER[nCurentLanguage - 1];
                lbTagtile.Text =  MultipleLanguage.PRODUCT_TAG[nCurentLanguage - 1] + " :";



            }
        }
        catch
        {
            string sURL = Config.ROOT + Pages.FrontEnds.HOME;
            Response.Redirect(sURL);
        }
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.NEWS_ID]);
        NewsComment objNewsComment = new NewsComment();
        objNewsComment.Data.Content = ConvertData.ConvertToString(txtContent.Text);
        objNewsComment.Data.Dateposted = ConvertData.ConvertDatetimeToInt(DateTime.Today);
        objNewsComment.Data.NewsID = nItemID;
        objNewsComment.Data.Status = EnumeType.INACTIVE;
        objNewsComment.Data.Email = ConvertData.ConvertToString(txtEmail.Text);
        int nResult = objNewsComment.Insert();
        //if (nResult > 0)
        //{
        //    Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nCurrentLang - 1]);
        //    txtContent.Text = string.Empty;
        //}
        //else
        //{
        //    Alert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[nCurrentLang - 1]);
        //}
        Response.Redirect(System.Web.HttpContext.Current.Request.Url.AbsoluteUri);
    }
    #region Repeater ptComment

    protected void rptComment_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }
    protected void rptComment_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        int nCurrentLanguage = Support.GetCurrentLanguage();
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblCommentDetail = (Label)e.Item.FindControl("lblCommentDetail");
            Label lblEmail = (Label)e.Item.FindControl("lblEmail");

            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "CommentID"));
            int nNewsID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "NewsID"));

            lblCommentDetail.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Content"));
            int nPostedDate = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Dateposted"));
            string sEmail = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Email"));
            lblEmail.Text = ConvertData.ConvertIntToDatetimeString(nPostedDate) + "," + " " + MultipleLanguage.BY[nCurrentLanguage - 1] + " " + sEmail;
        }
    }

    #endregion

    #region Repeater rptRelatedNews

  
    protected void rptRelatedNews_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string sCommand = e.CommandName;
        int nNewsID = ConvertData.ConvertToInt(e.CommandArgument);
        switch (sCommand)
        {
            case "newsrelated":
                string sNewsDetail = Config.ROOT + Pages.FrontEnds.NEWS_DETAIL + "?" + Config.NEWS_ID + "=" + nNewsID;
                Response.Redirect(sNewsDetail);
                break;
        }
    }
    protected void rptRelatedNews_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            LinkButton lbtRelated = (LinkButton)e.Item.FindControl("lbtRelated");
            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "NewsID"));
            lbtRelated.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "NewsTitle"));
            lbtRelated.PostBackUrl = Config.ROOT + Pages.FrontEnds.NEWS_DETAIL + "?" + Config.NEWS_ID + "=" + nID;
            lbtRelated.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "NewsID"));
        }
    }
    #endregion
}